# @Time : 2024-11-02 18:11
# @Author : Fioman 
# @Phone : 13149920693
"""
使用multiprocessing.Value和multiprocessing.Array实现
Value用于共享一个单独的值,Array用于共享一个数组
"""
import time
from multiprocessing import Value, Array, Process


def modify_value(val, array):
    val.value = 3.13333
    for i in range(len(array)):
        array[i] = -array[i] + 10


def print_value(val, array):
    time.sleep(1)
    print("进程2中打印")
    print(val.value)
    print(array[:])
    time.sleep(1)


if __name__ == '__main__':
    sharedValue = Value('d', 0.0)
    sharedArray = Array('i', range(10))
    p = Process(target=modify_value, args=(sharedValue, sharedArray))
    p.start()
    p2 = Process(target=print_value, args=(sharedValue, sharedArray))
    p2.start()
    p.join()
    p2.join()
    print("主进程中打印")
    print(sharedValue.value)
    print(sharedArray[:])
